<?php

namespace app\backend\controller;
use app\backend\service\ZhTopSetService;
use app\backend\model\ZhTopVideo as ZhTopVideoModel;
use app\backend\service\ZhTopVideoService;

class ZhTopVideo extends Admin
{
    /*首页数据列表*/
    function index()
    {
        if (!$this->request->isAjax()) {
            $res = ZhTopVideoService::indexList([], 100, 1);
            $this->view->assign('level_rows', $res['rows']);
            return view('index');
        } else {
            $limit  = $this->request->post('limit', 20, 'intval');
            $offset = $this->request->post('offset', 0, 'intval');
            $page   = floor($offset / $limit) + 1;
            $where                        = [];
            $where['title']              = $this->request->param('title', '', 'serach_in');
            $res = ZhTopVideoService::indexList([],  $limit, $page);
            return json($res);
        }
    }

    /*修改*/
    function update()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get('id', '', 'serach_in');
            if (!$id) $this->error('参数错误');
            $info   = ZhTopVideoModel::find($id)->toArray();

            $res = ZhTopVideoService::indexList([], 100, 1);

            $this->view->assign('level_rows', $res['rows']);
            $this->view->assign('info', checkData($info));
            return view('update');
        } else {
            $postField = 'id,title,videos,videos2';
            $data      = $this->request->only(explode(',', $postField), 'post', null);
            $res     = ZhTopVideoService::update($data);
            return json(['status' => '00', 'msg' => '修改成功']);
        }
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @author  lwx
     */
    function add()
    {
        if (!$this->request->isPost()) {
            $res = ZhTopVideoService::indexList([] , 100, 1);
            $this->view->assign('level_rows', $res['rows']);
            return view('add');
        } else {
            $postField = 'id,title,videos,videos2';
            $data               = $this->request->only(explode(',', $postField), 'post', null);
            $data['createtime'] = time();
            $data['wxapp_id']   = session('backend.wxapp_id');
            $res = ZhTopVideoModel::create($data);
            return json(['status' => '00', 'msg' => '添加成功']);
        }
    }

    /*删除*/
    function delete()
    {
        $idx = $this->request->post('id', '', 'serach_in');
        if (!$idx) $this->error('参数错误');
        try {
            ZhTopVideoModel::destroy(['id' => explode(',', $idx)], true);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }

    /*查看详情*/
    function view()
    {
        $res = ZhTopVideoModel::indexList([], 100, 1);
        $this->view->assign('level_rows', $res['rows']);
        $id = $this->request->get('id', '', 'serach_in');
        if (!$id) $this->error('参数错误');
        $info                         = \app\backend\model\ZhTopVideoModel::find($id)->toArray();
        $info['videos']               = json_decode(htmlspecialchars_decode($info['videos']), true);
        $this->view->assign('info', checkData($info));
        return view('view');
    }
}